package cn.kbyue.adminclientdemo.service;

import cn.kbyue.adminclientdemo.entity.UserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @author xlang
 * @date 2023/4/4 14:49
 */
@Slf4j
@Service
public class CacheService {

    @Cacheable(cacheNames = "usercache", key = "#id")
    public UserDTO getById(Integer id) {

        log.info("[CacheService getById()] Emulate query from database.");

        UserDTO userDTO = new UserDTO();
        userDTO.setId(id);
        userDTO.setUsername("name-" + id);
        return userDTO;
    }

    @CacheEvict(cacheNames = "usercache", allEntries = true)
    public boolean delete(Integer id) {
        return true;
    }

}
